const stringifyForLog = (value, max = 800) => {
  if (value === undefined || value === null) {
    return value;
  }
  if (typeof value === 'string') {
    return value.length > max ? `${value.slice(0, max)}...` : value;
  }
  try {
    const str = JSON.stringify(value);
    return str.length > max ? `${str.slice(0, max)}...` : str;
  } catch (error) {
    return String(value);
  }
};

import { sanitizeChromeWebContentResult } from '../lib/chrome-web-content.js';

export const chromeGetWebContentTool = {
  name: 'chrome_get_web_content',
  async doCall({ mcpClient, args, toolName }) {
    console.log('[Tools][chrome_get_web_content] doCall args', { toolName, args: stringifyForLog(args) });
    const result = await mcpClient.callTool(toolName, args);
    console.log('[Tools][chrome_get_web_content] doCall result', stringifyForLog(result));
    return result;
  },
  async getToolRawResult(callResult) {
    console.log('[Tools][chrome_get_web_content] getToolRawResult input', stringifyForLog(callResult));
    return callResult;
  },
  async getToolTextResult(rawResult) {
    console.log('[Tools][chrome_get_web_content] getToolTextResult input', stringifyForLog(rawResult));
    const sanitized = sanitizeChromeWebContentResult(rawResult);
    console.log('[Tools][chrome_get_web_content] getToolTextResult output', stringifyForLog(sanitized));
    return sanitized;
  }
};
